package hittegods.classes;

import hittegods.database.MySQLConn;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Reward {

    public final static int SUCCESS = 1;
    public final static int ERROR = 2;
    public final static int SQL_EXCEPTION = 3;
    public final static int UNDEFINED_ERROR = 4;
    private double value;
    private double reqValue;
    private Date datePaid;
    private String caseNumber;

    public Reward(String caseNumber) {
        try {
            Statement stmt = MySQLConn.getConnection().createStatement();
            this.caseNumber = caseNumber;
            String sql = "SELECT rewardValue, reqValue, paidDate FROM reward WHERE caseNo='" + caseNumber + "'";
            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                datePaid = rs.getDate("paidDate");
                reqValue = rs.getDouble("reqValue");
                value = rs.getDouble("rewardValue");
            }
            rs.close();
        } catch (SQLException ex) {
        } catch (Exception ex) {
        }
    }

    public static int insertReward(String caseNo, String reqValue) {
        int result = UNDEFINED_ERROR;
        try {
            String sql = "INSERT INTO reward (caseNo, reqValue) VALUES('" + caseNo + "','" + reqValue + "')";
            Statement stmt = MySQLConn.getConnection().createStatement();
            stmt.execute(sql);
            result = SUCCESS;
        } catch (SQLException ex) {
            return SQL_EXCEPTION;
        } catch (Exception ex) {
            return UNDEFINED_ERROR;
        }
        return result;
    }

    public int updateReward(String rewardValue) {
        int result = UNDEFINED_ERROR;
        Date date = new Date(System.nanoTime());
        try {
            String sql = "UPDATE reward SET rewardValue='" + rewardValue + "', paidDate='" + date + "' WHERE caseNo='" + caseNumber + "'";
            Statement stmt = MySQLConn.getConnection().createStatement();
            stmt.execute(sql);
            result = SUCCESS;
        } catch (Exception ex) {
            return UNDEFINED_ERROR;
        }
        return result;
    }

    public String getCaseNumber() {
        return caseNumber;
    }

    public Date getDatePaid() {
        return datePaid;
    }

    public double getReqValue() {
        return reqValue;
    }

    public double getValue() {
        return value;
    }

    public String toString() {
        return caseNumber + "\n" + datePaid + "\n" + reqValue + "\n" + value;
    }
}